<?php
/**
 * @orm tablename users_action_log
 * @orm index(user,userClassName,date,action)
 */
class DomUserActionLog extends DomArObject {
	
	/**
	 * @orm plugin DomArDateTime(now)
	 */
	protected $date;
	/**
	 * @orm char(15) index
	 */
	protected $ip;
	/**
	 * @orm char(50)
	 */
	protected $userClassName;
	/**
	 * @orm int(11)
	 */
	protected $user;
	/**
	 * @var Int [2-logged in, 3-logged out, 4-used global password, 5-sent lost password email, 6-user confirmed email, 7-login by cookie]
	 * @orm int(11)
	 */
	protected $action = 0;
	/**
	 * @orm text
	 */
	protected $comment;
	
	/**
	 * Logs user action
	 *
	 * @param DomUser $user
	 * @param Int $action
	 * @param String $comment
	 */
	public static function log( DomUser $user = NULL, $action, $comment = '' ) {
		
		$log = new DomUserActionLog();
		$log->ip = ip();
		$log->user = $user->id;
		$log->userClassName = get_class($user);
		$log->action = $action;
		$log->comment = $comment;
		
		if( !$log->save() )
			throw new RuntimeException('Can\'t save user log entry: <br> '.implode('<br>', $log->getValidationReport()->messages ));
	}
	
}
?>